from rest_framework import mixins
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
from goods.models import SKUImage, SKU
from meiduo_admin.serializers.skus import SKUSimpleSerializer, SKUImageSerializer


class SKUImageViewSet(ModelViewSet):
    """
    请求方式： GET /meiduo_admin/skus/images/?page=<页码>&pagesize=<页容量>
    请求参数： 通过请求头传递jwt token数据。
    返回数据： JSON
    {
        "counts": "图片总数量",
        "lists'(图片数据)'": [
            {
                "id": "图片ID",
                "sku": "sku商品名称",
                "sku_id": "sku商品ID",
                "image": "sku图片地址"
            },
            ...
        ],
        "page": "页码",
        "pages": "总页数",
        "pagesize": "页容量"
    }
    """
    # 仅管理员才能进行访问
    permission_classes = [IsAdminUser]

    # 指定视图所使用的查询集
    queryset = SKUImage.objects.all()
    # 指定视图所使用的序列化器类
    # serializer_class = '图片对象的序列化器类'
    serializer_class = SKUImageSerializer

    # 指定router动态生成路由时,提取参数的正则表达式
    lookup_value_regex = '\d+'

    # 获取图片数据GET /meiduo_admin/skus/images/ -> list
    # 新增图片数据 POST /meiduo_admin/skus/images/ -> create
    # 获取指定图片数据 GET /meiduo_admin/skus/images/(?P<pk>\d+)/ -> retrieve
    # 修改指定图片数据 PUT /meiduo_admin/skus/images/(?P<pk>\d+)/ -> update
    # 删除指定图片数据 DELETE /meiduo_admin/skus/images/(?P<pk>\d+)/ -> destroy

    # def list(self, request, *args, **kwargs):
    #     queryset = self.get_queryset()
    #     serializer = self.get_serializer(queryset, many=True)
    #     return Response(serializer.data)

    # def create(self, request, *args, **kwargs):
    #     serializer = self.get_serializer(data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #     serializer.save() # 调用序列化器类中的create
    #     return Response(serializer.data, status=201)

    # def retrieve(self, request, *args, **kwargs):
    #     instance = self.get_object()
    #     serializer = self.get_serializer(instance)
    #     return Response(serializer.data)

    # def update(self, request, *args, **kwargs):
    #     instance = self.get_object()
    #     serializer = self.get_serializer(instance, data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #     serializer.save() # 调用序列化器类中的update
    #     return Response(serializer.data)

    # def destroy(self, request, *args, **kwargs):
    #     instance = self.get_object()
    #     instance.delete() # 思考：FDFS中的图片会不会被删除？不会
    #     return Response(status=204)


class SKUSimpleView(ListAPIView):
    """
    请求方式： GET /meiduo_admin/skus/simple/
    请求参数： 通过请求头传递jwt token数据
    返回数据： JSON
    [
        {
            "id": "sku商品ID",
            "name": "sku商品名称",
        },
        ...
    ]
    """
    permission_classes = [IsAdminUser]

    # 指定视图所使用的查询集
    queryset = SKU.objects.all()
    # 指定视图所使用的序列化器类
    serializer_class = SKUSimpleSerializer

    # 当前视图不分页:关闭分页
    pagination_class = None
